﻿using UnityEngine;
using System;
using System.Collections.Generic;

public static class DataManager
{

    public static readonly string urlVideo = "https://www.yidiudiu.org/heikiz/cfg/forTest/cfgVideo.txt";
    public static readonly string urlGame = "https://www.yidiudiu.org/heikiz/cfg/forTest/cfgGame.txt";
    public static readonly string urlLesson = "https://www.yidiudiu.org/heikiz/cfg/forTest/cfgLesson.txt";
    public static readonly string urlYiguzuoqi = "https://www.yidiudiu.org/heikiz/cfg/forTest/cfgYiguzuoqi.txt";
    public static readonly string urlKejian = "https://www.yidiudiu.org/heikiz/cfg/forTest/cfgKejian.txt";
    public static readonly string urlUrls = "https://www.yidiudiu.org/heikiz/cfg/forTest/cfgUrls.txt";


    public static VideoCfgData videoCfgData = new VideoCfgData ();
	public static GameCfgData gameCfgData = new GameCfgData ();
	public static LessonCfgData lessonCfgData = new LessonCfgData ();
    public static YiguzuoqiCfgData yiguzuoqiCfgData = new YiguzuoqiCfgData();
    public static KejianCfgData kejianCfgData = new KejianCfgData();
    public static UrlCfgData urlCfgData = new UrlCfgData();
    public static SchoolData schoolData = new SchoolData();
    public static AppUserData appUserData = new AppUserData();
    public static SchoolsData schoolsData = new SchoolsData();

	public static bool Init( Action finishCallback )
	{
        Screen.sleepTimeout = SleepTimeout.NeverSleep;

		Loading.GetInstance ().StartLoading ();

		Debug.Log ("start init data............");

		int count = 0;
        // read videoCfg
        
//            ResLoader.Load("file://" + ResLoader.Url2LocalPath(urlVideo), null, (www) => {
        ResLoader.Load (urlVideo, null, (www) => {

			if (!string.IsNullOrEmpty( www.error ))
			{
				Debug.Log("read cfgVideo error");
			}
			else
			{
				videoCfgData = new VideoCfgData ();
				videoCfgData.Reset (www.text);

				Debug.Log ("finish init video data............");
			}

			count++;
			if ( count >=5 && finishCallback != null )
			{
				Loading.GetInstance().EndLoading();
				finishCallback();
			}

		}, false, true);


        // read gameCfg
//        ResLoader.Load("file://" + ResLoader.Url2LocalPath(urlGame), null, (www) => {
    		ResLoader.Load (urlGame, null, (www) => {

            if ( !string.IsNullOrEmpty( www.error ))
			{
				Debug.Log("read cfgGame error");
			}
			else
			{
				gameCfgData = new GameCfgData ();
				gameCfgData.Reset (www.text);

				Debug.Log ("finish init game data............");
			}

			count++;
			if ( count >=5 && finishCallback != null )
			{
				Loading.GetInstance().EndLoading();
				finishCallback();
			}

		}, false, true);


        // read lessonCfg
//        ResLoader.Load("file://" + ResLoader.Url2LocalPath(urlLesson), null, (www) => {
    		ResLoader.Load (urlLesson, null, (www) => {

            if (!string.IsNullOrEmpty( www.error ) )
			{
				Debug.Log("read cfgLesson error");
			}
			else
			{
				lessonCfgData = new LessonCfgData ();
				lessonCfgData.Reset (www.text);

				Debug.Log ("finish init lesson data............");
			}

			count++;
			if ( count >=5 && finishCallback != null )
			{
				Loading.GetInstance().EndLoading();
				finishCallback();
			}

		}, false, true);


        // read yiguzuoqiCfg
//        ResLoader.Load("file://" + ResLoader.Url2LocalPath(urlYiguzuoqi), null, (www) => {
    		ResLoader.Load (urlYiguzuoqi, null, (www) => {

            if (!string.IsNullOrEmpty(www.error))
            {
                Debug.Log("read cfgYiguzuoqi error");
            }
            else
            {
                yiguzuoqiCfgData = new YiguzuoqiCfgData();
                yiguzuoqiCfgData.Reset(www.text);

                Debug.Log("finish init yiguzuoqi data............");
            }

            count++;
            if (count >= 5 && finishCallback != null)
            {
                Loading.GetInstance().EndLoading();
                finishCallback();
            }

        }, false, true);

        // read kejianCfg
//        ResLoader.Load("file://" + ResLoader.Url2LocalPath(urlKejian), null, (www) => {
    		ResLoader.Load (urlKejian, null, (www) => {

            if (!string.IsNullOrEmpty(www.error))
            {
                Debug.Log("read cfgKejian error");
            }
            else
            {
                kejianCfgData = new KejianCfgData();
                kejianCfgData.Reset(www.text);

                Debug.Log("finish init kejian data............");
            }

            count++;
            if (count >= 5 && finishCallback != null)
            {
                Loading.GetInstance().EndLoading();
                finishCallback();
            }

        }, false, true);

        // read urlCfg
        /*        ResLoader.Load(urlUrls, null, (www) => {

                    if (!string.IsNullOrEmpty(www.error))
                    {
                        Debug.Log("read cfgUrl error");
                    }
                    else
                    {
                        urlCfgData = new UrlCfgData();
                        urlCfgData.Reset(www.text);

                        Debug.Log("finish init url data............");
                    }

                    count++;
                    if (count >= 4 && finishCallback != null)
                    {
                        Loading.GetInstance().EndLoading();
                        finishCallback();
                    }

                }, false, true);*/


        NetMgr.ReqAllSchool();

        // unload all resources at last
        Resources.UnloadUnusedAssets();

		return true;

	}

	public static string LoadResourcesCfg( string file )
	{
		TextAsset  txt = (TextAsset)Resources.Load(file);
		//用加载得到的资源对象，实例化游戏对象，实现游戏物体的动态加载

		string v = "";
		if (txt != null)
			v = txt.text;

		return v;
	}
}

